/*
 * @lc app=leetcode.cn id=238 lang=typescript
 *
 * [238] 除自身以外数组的乘积
 */

// @lc code=start
// O(n) 禁用除法
function productExceptSelf(nums: number[]): number[] {
  const N = nums.length
  const answer = new Array<number>(N)
  // answer[i] = L * R
  answer[0] = 1
  for (let i = 1; i < N; i++) {
    answer[i] = answer[i - 1] * nums[i - 1]
  }
  let R = 1
  for (let i = N - 1; i >= 0; i--) {
    answer[i] *= R
    R *= nums[i]
  }
  return answer
}
// @lc code=end
